<div class="<%= stimulus_id %>" data-controller="<%= stimulus_id %>">
  <%= turbo_frame_tag "edit_order_#{params[:type]}_address_modal" do %>
    <%= render component("ui/modal").new(title: t(".title.#{@type}")) do |modal| %>
      <%= solidus_form_for @order, url: solidus_admin.send("order_#{@type}_address_path", @order), html: { id: form_id } do |form| %>
        <div class="w-full flex flex-col mb-4">
          <div class="flex justify-between items-center mb-4 relative">
            <h2 class="text-sm font-semibold">
              <%= t(".subtitle.#{@type}") %>
            </h2>

            <% if @addresses.present? %>
              <%= render component('ui/dropdown').new(
                text: t(".select_address"),
                "data-#{stimulus_id}-target": "addresses",
                class: "max-h-[26rem] overflow-y-auto"
              ) do %>
                <% @addresses.each do |address| %>
                  <%= tag.a(
                    format_address(address),
                    href: solidus_admin.send("order_#{@type}_address_path", @order, address_id: address.id),
                    'data-turbo-frame': address_frame_id,
                    'data-action': "#{component('ui/dropdown').stimulus_id}#close",
                  ) %>
                <% end %>
              <% end %>
            <% end %>
          </div>

          <div class="w-full flex gap-4">
            <%= turbo_frame_tag address_frame_id do %>
              <%= render component('ui/forms/address').new(addressable: @address, form_field_name: "order[#{@type}_address_attributes]") %>
            <% end %>
          </div>

          <%= form.checkbox(use_attribute,
            checked: @address == (@type == 'ship' ? @order.bill_address : @order.ship_address),
            label: t(".use_this_address.#{@type}"),
            label_options: { size: :xs })
          %>
        </div>
      <% end %>

      <% modal.with_actions do %>
        <form method="dialog">
          <%= render component("ui/button").new(
            scheme: :secondary,
            text: t(".cancel"),
          ) %>
        </form>

        <%= render component("ui/button").save(form: form_id) %>
      <% end %>
    <% end %>
  <% end %>

  <%= render component("orders/show").new(order: @order) %>
</div>
